﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace GetTouringPlansData
{
    class Program
    {
        static void Main(string[] args)
        {
            SeedData();
            
            populateAttractions();
            
            populateAttractionDetails();

            populateParkDining();

            populateParkDiningDetail();

        }

        private static void populateParkDiningDetail()
        {
            using (var db = new DisneyContext())
            {
                var d = from pk in db.Parks
                    from din in pk.Dinings
                    select new {dining = din, park = pk};

                foreach (var detail in d)
                {
                    string jsonDining = string.Empty;
                    jsonDining = new TouringPlansData().GetDiningDetail(detail.park.Permalink,
                                                                        detail.dining.permalink);
                    var parkDining = JsonConvert.DeserializeObject<DiningDetail>(jsonDining);

                    detail.dining.DiningDetail = parkDining;
                }

                db.SaveChanges();
            }
        }

        private static void populateParkDining()
        {
            using (var db = new DisneyContext())
            {
                var allParks = from p in db.Parks.Include("Dinings")
                               select p;

                string jsonDining = string.Empty;
                foreach (var park in allParks)
                {
                    jsonDining = new TouringPlansData().GetParkDinings(park.Permalink);
                    jsonDining = jsonDining.Replace("[[", "[");
                    jsonDining = jsonDining.Replace("]]", "]");
                    jsonDining = jsonDining.Replace("],[",",");

                    var parkDining = JsonConvert.DeserializeObject<List<Dining>>(jsonDining);
                    foreach (var dining in parkDining)
                    {
                        park.Dinings.Add(dining);
                    }
                }
                db.SaveChanges();
            }
        }

        private static void SeedData()
        {
            using (var db = new DisneyContext())
            {
                var park = new Park { Name = "Walt Disney World" };
                park.Permalink = "magic-kingdom";
                db.Parks.Add(park);

                park = new Park { Name = "Epcot" };
                park.Permalink = "epcot";
                db.Parks.Add(park);

                park = new Park { Name = "Animal Kingdom" };
                park.Permalink = "animal-kingdom";
                db.Parks.Add(park);

                park = new Park { Name = "Hollywood Studios" };
                park.Permalink = "hollywood-studios";
                db.Parks.Add(park);

                park = new Park { Name = "Typhoon Lagoon" };
                park.Permalink = "typhoon-lagoon";
                db.Parks.Add(park);

                park = new Park { Name = "Blizzard Beach" };
                park.Permalink = "blizzard-beach";
                db.Parks.Add(park);

                db.SaveChanges();
            }
        }

        private static void populateAttractionDetails()
        {
            using (var db = new DisneyContext())
            {                
                var attractions = from pk in db.Parks
                                  from att in pk.Attractions
                                  select new {attraction = att, park = pk};

                foreach (var attraction in attractions)
                {
                    string attractionDetailData = new TouringPlansData()
                                                        .GetAttractionDetail(attraction.park.Permalink,
                                                                            attraction.attraction.Permalink);

                    var attractionDetail = JsonConvert.DeserializeObject<AttractionDetail>(attractionDetailData);
                    attraction.attraction.AttractionDetail = attractionDetail;
	            }

                db.SaveChanges();
            }            
        }

        private static void populateAttractions()
        {
            using (var db = new DisneyContext())
            {
                var allParks = from p in db.Parks.Include("Attractions")
                               select p;

                string jsonAttractions = string.Empty;
                foreach (var park in allParks)
                {
                    jsonAttractions = new TouringPlansData().GetAttractions(park.Permalink);
                    var parkAttractions = JsonConvert.DeserializeObject<List<Attraction>>(jsonAttractions);
                    foreach (var attraction in parkAttractions)
                    {
                        attraction.Park = park;
                        park.Attractions.Add(attraction);
                    }
                }
                db.SaveChanges();
            }
        }
    }
}

